<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {

            position: fixed;
            /* position: absolute; */
            top: 100px;
            left: 100px;
            width: 200px;
            height: 200px;
            background-color: yellow;
            opacity: 0.5;
        }
    </style>
</head>

<body>
    <div></div>
</body>
<script>
    window.innerWidth
    window.innerHeight
    var div = document.querySelector('div')
    console.dir(div)
    var temp = parseInt(div.offsetTop)
    var width = parseInt(div.offsetLeft)
    document.addEventListener('keydown', function (e) {
        if (e.keyCode == 87) {
            temp -= 10
            if (temp <= 0) {
                temp = 0
                div.style.top = temp + 'px'
            }

            else {
                div.style.top = temp + 'px'
            }

        }
        else if (e.keyCode == 83) {
            temp += 10

            if (temp >= window.innerHeight - 200) {
                temp = window.innerHeight - 200
                div.style.top = temp + 'px'
            }
            else {
                div.style.top = temp + 'px'
            }

        }
        else if (e.keyCode == 65) {
            width -= 10
            if (width <= 0) {
                width = 0
                div.style.left = width + 'px'
            }

            else {
                div.style.left = width + 'px'
            }
        }
        else if (e.keyCode == 68) {
            width += 10
            if (width >= window.innerWidth - 200) {
                width = window.innerWidth - 200
                div.style.left = width + 'px'
            }
            else {
                div.style.left = width + 'px'
            }
        }

    })
</script>

</html>